Jeg plejer at bruge str_replace:
$tekst = str_replace("[b]", "<b>", $tekst);
$tekst = str_replace("[/b]", "</b>", $tekst);
$tekst = str_replace("[i]", "<i>", $tekst);
$tekst = str_replace("[/i]", "</i>", $tekst);
Og så videre. Til at formatere links bruger jeg dette script:
<?php
/*
File : tagster_lib.php
Version : 3.0
Date : September 22nd, 2002
Author : Lars B. Jensen, lars.jensen@ljweb.com
Module Description
Module to transform URL and E-Mail addresses into clickable links.
Instruction
To utilize the tagster_lib library, copy this file to a location on your server. Include the
library into your php document or template.
Use the function tagster_format to extract and modify all links and emails in a given string.
The parameters $target handling the target for links and $class for handling css on <a tags are
both optional.
Example
include('lib/tagster_lib.php');
$str = tagster_format($str, "_blank", "mystyle");
Note
This module library has support for the scandinavian specialchars æ, ø and å.
Public Functions
--------------------------------------------------------------´
function tagster_format($str, $target, $css_class, $fixlongword)
function tagster_url($str, $target, $class)
function tagster_email($str, $class)
Private Functions
--------------------------------------------------------------´
function tagster_fix_endchar($str)
function tagster_expand($str)
function tagster_reduce($str)
function tagster_fix_longword($str, $maxlen)
*/
function tagster_format($str, $target="_blank", $css_class="", $fixlongword = 0) {
$str = str_replace("&", "&", $str);
$str = str_replace("<", "<", $str);
$str = str_replace(">", ">", $str);
$str = tagster_url($str, $target, $css_class);
$str = tagster_email($str, $css_class);
$str = tagster_fix_endchar($str);
$str = str_replace(" ", " ", $str);
$str = str_replace("\\t", " ", $str);
$str = str_replace("\\r", "", $str);
$str = str_replace("\\n", "<br>", $str);
if ($fixlongword != 0) $str = tagster_fix_longword($str, $fixlongword);
return $str;
}
function tagster_url($str, $target, $css_class) {
$ins_str = "";
if ($css_class) $ins_str .= " class=\\"".$css_class."\\"";
if ($target) $ins_str .= " target=\\"".$target."\\"";
$str = tagster_expand($str);
$str = preg_replace ("/(ftp|http|https|telnet|news|nntp|file|irc):\\/\\/([a-z0-9~#%@&:;=!',_æøå\\(\\)\\?\\/\\.\\-\\+\\[\\]\\|\\*\\$\\^\\{\\}]+)/i", "<a href=\\"\\\\1://\\\\2\\"".$ins_str.">\\\\1://\\\\2</a>", $str);
$str = preg_replace ("/(\\s|tp\\:|\\(|\\[|\\>)(www\\.)([a-z0-9~#%@&:;=!',_æøå\\(\\)\\?\\/\\.\\-\\+\\[\\]\\|\\*\\$\\^\\{\\}]+)/i", "\\\\1<a href=\\"http://\\\\2\\\\3\\"".$ins_str.">\\\\2\\\\3</a>", $str);
$str = preg_replace ("/(\\s|tp\\:|\\(|\\[|\\>)(ftp\\.)([a-z0-9~#%@&:;=!',_æøå\\(\\)\\?\\/\\.\\-\\+\\[\\]\\|\\*\\$\\^\\{\\}]+)/i", "\\\\1<a href=\\"ftp://\\\\2\\\\3\\"".$ins_str.">\\\\2\\\\3</a>", $str);
return tagster_reduce($str);
}
function tagster_email($str, $css_class="") {
$ins_class = "";
if ($css_class) $ins_class = " class=\\"".$css_class."\\"";
$str = tagster_expand($str);
$str = preg_replace ("/([\\s\\"])([\\w\\.\\-_]+)@([\\w\\-_]+)\\.([\\w\\.\\-_]+)/i", "\\\\1<a href=\\"mailto:\\\\2@\\\\3.\\\\4\\"".$ins_class.">\\\\2@\\\\3.\\\\4</a>", $str);
return tagster_reduce($str);
}
function tagster_fix_endchar($str) {
$str = preg_replace ("/([\\'\\"\\)\\]\\.\\,\\?\\!]+)\\">/i", "\\">", $str);
$str = preg_replace ("/([\\'\\"\\)\\]\\.\\,\\?\\!]+)\\" (target|class)=\\"/i", "\\" \\\\2=\\"", $str);
$str = preg_replace ("/([\\'\\"\\)\\]\\.\\,\\?\\!]+)<\\/a>/i", "</a>\\\\1", $str);
return $str;
}
function tagster_fix_longword($str, $maxlen = 10) {
$sw = 0;
$in_tag = false;
$str_size = strlen($str);
$res = "";
for ($i=0; $i<$str_size; $i++){
if ($str[$i] == '<') $in_tag = true;
else if ($str[$i] == ' ') $sw = 0;
if (!$in_tag && (($sw++) > $maxlen)) {
$res .= '';
$sw = 0;
}
if ($str[$i] == '>') $in_tag = false;
$res .= $str[$i];
}
return $res;
}
function tagster_expand($str) {
return " ".$str." ";
}
function tagster_reduce($str) {
return substr($str, 1, -1);
}
?>
Læs hvad der står øverst, så giver den sig selv.